package com.moleq.dialog;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.BottleSetting;

public class BottleEditDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private Button btn_bottle_ok,btn_bottle_cancel;
	private EditText et_bottleno, et_bottledesc, et_bottleamount;
	private LinearLayout ll_dialog_perview_mode_layout;
	private Context mContext;
	private List<BottleSetting> listBottle;
	private BottleSetting mBottleSetting;
	private MessageDialog mMessageDialog;
	private String action = "";
	
	public void onCloseWithReturn(int what,Object obj){
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}

	public BottleEditDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<BottleSetting> listBottle,BottleSetting bottle,String action){
		this.listBottle = listBottle;
		this.mBottleSetting = bottle;
		this.action = action;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public BottleEditDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_edit_bottle);
		this.setCancelable(false);
	
		ll_dialog_perview_mode_layout = (LinearLayout) findViewById(R.id.ll_dialog_perview_mode_layout);
		et_bottleno = (EditText) findViewById(R.id.et_bottleno);
		et_bottledesc = (EditText) findViewById(R.id.et_bottledesc);
		et_bottleamount = (EditText) findViewById(R.id.et_bottleamount);
		
		btn_bottle_ok = (Button) findViewById(R.id.btn_bottle_ok);
		btn_bottle_cancel = (Button) findViewById(R.id.btn_bottle_cancel);
		ll_dialog_perview_mode_layout.setOnClickListener(OnClickDialogWindowListener);
		btn_bottle_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_bottle_cancel.setOnClickListener(OnClickDialogWindowListener);
	}
	
	public void show(){
		super.show();	
		getDetail(mBottleSetting);
		et_bottledesc.requestFocus();
	}
	
	public void getDetail(BottleSetting bottle){
//		et_bottleno.setText(String.valueOf(bottle.getBo_no()));
		et_bottleno.setText(String.valueOf(bottle.getNo()));
		et_bottledesc.setText(bottle.getBo_desc());
		double amount = bottle.getBo_amt();
		if (amount != 0) {
			et_bottleamount.setText(String.valueOf(amount));
		}else{
			et_bottleamount.setText("");
		}
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_bottle_ok:
				if (validation() == false) return;
				
				mBottleSetting.setBo_desc(et_bottledesc.getText().toString());
				mBottleSetting.setBo_amt(Double.valueOf(et_bottleamount.getText().toString()));
				if (mBottleSetting.getType().equals("I") && action.equals("add")) {
					listBottle.add(mBottleSetting);
				}
				mBottleSetting.setEdit(true);
				
				onCloseWithReturn(R.id.btn_bottle_ok, listBottle);
				dismiss();
				break;
			case R.id.btn_bottle_cancel:
				dismiss();
				break;
			case R.id.ll_dialog_perview_mode_layout:
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(BottleEditDialog.this
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			}
		}
	};
	
	public boolean validation(){
		boolean bo = true;
		String amount = et_bottleamount.getText().toString().trim();
		if (et_bottledesc.getText().toString().trim().equals("")) {
			ShowMessage("Desc can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottledesc.requestFocus();
			bo = false;
		}else if (et_bottleamount.getText().toString().trim().equals("")) {
			ShowMessage("Amount can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottleamount.requestFocus();
			bo = false;
		}else if (et_bottleamount.getText().toString().trim().equals(".")) {
			ShowMessage("Amount is not valid.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottleamount.requestFocus();
			bo = false;
		}else if (et_bottleamount.getText().toString().trim().equals("0")) {
			ShowMessage("Amount can not be zero.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottleamount.requestFocus();
			bo = false;
		}else if (Double.valueOf(amount) <= 0) {
			ShowMessage("Amount can not be zero.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottleamount.requestFocus();
			bo = false;
		}else if (checkDouble(amount)) {
			ShowMessage("Amount is not valid.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_bottleamount.requestFocus();
			bo = false;
		}
		return bo;
	}
	
	private boolean checkDouble(String num){
		int index = num.indexOf(".");
		String temp = num.substring(index+1);
		int len = temp.length();
		if (len > 2) {
			return true;
		}
		return false;
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
